<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Iphone_api extends CI_Controller
{
	public function  __construct()
	{	
		parent::__construct();
		$this->load->model("personal_health_info_model", "health_info_model");
		$this->load->model("customer_friend_model", "friend_model");
		$this->load->model("customer_model");
		$this->load->model("customer_health_model", "health_model");
	}
	
	public function get_personal_health_info($seq, $uid)
	{
		$health_info = $this->health_info_model->get_current_health_info($uid);
	
		if(!$health_info){
			echo $seq.'|';
			return;
		}
	
		echo $seq.'|';
		echo json_encode($health_info);
	}
	
	public function get_my_uid($seq)
	{
		echo $seq.'|';
		echo get_uid();
	}
	
	public function get_health_field_map($seq)
	{
		$this->db->select("columnName, description");
		$query = $this->db->get("healthfieldmeta");
		if(!$query->num_rows()){
			echo $seq.'|';
			return;
		}

		$map = array();
		for($i = 0; $i < $query->num_rows(); $i ++)
		{
			$row = $query->row_array($i);
			$field = $row["columnName"];
			$description = $row["description"];
			
			$map[$field] = $description;
		}
		
		echo $seq.'|';
		echo json_encode($map);
	}
	
	public function get_friend_request($seq, $uid)
	{
		$all_req = $this->friend_model->get_all_req();
		
		if(!$all_req){
			echo "";
			return;
		}
		
		echo $seq.'|';
		echo json_encode($all_req);
	}
	
	public function get_base_info($seq, $uid)
	{
		$result = $this->customer_model->get_base_info($uid);
		
		if(!$result)
		{
			echo $seq.'|';
			return;
		}
		
		echo $seq.'|';
		echo json_encode($result);
	}
	
	public function get_shared_symptom($seq, $uid)
	{
		$result = $this->health_info_model->get_shared_symptom($uid);
		
		if(!$result){
			echo $seq.'|';
			return;
		}
		
		echo $seq.'|';
		echo json_encode($result);
	}
	
	public function get_all_symptom($seq, $uid)
	{
		$result = $this->health_info_model->get_last_symptoms($uid);
		if(!$result)
		{
			echo $seq.'|';
			return;
		}
		
		echo $seq.'|';
		echo json_encode($result);
	}
	
	public function get_user_friends($seq, $uid)
	{
		$all_friends = $this->friend_model->get_user_friends($uid);
		
		if(!$all_friends)
		{
			echo $seq.'|';
			return;
		}
		
		echo $seq.'|';
		echo json_encode($all_friends);
	}
	
	public function get_symptom_map($seq)
	{
		$ary = get_symptom_map();
		
		echo $seq.'|';
		echo json_encode($ary);
	}
	
	public function update_health_info($seq, $uid, $field, $value)
	{
		$this->health_model->update_health_data($uid, $field, $value);
		echo $seq.'|';
	}
	
	public function handle_friend_req($seq, $req_uid, $accept)
	{
		if($accept=="true")
		{
			$this->friend_model->accept_friend_req($req_uid);
		}
		else
		{
			$this->friend_model->deny_friend_req($req_uid);
		}
		
		echo $seq.'|';
	}
	
	public function update_symptom_share($seq, $uid, $symptom_type, $shared)
	{
		
		$this->health_info_model->share_symptom($uid, $symptom_type, $shared);
		echo $seq.'|';
	}
	
	public function add_symptom($seq, $uid, $symptom_type, $shared)
	{
		if($shared == "true")
			$shared = 1;
		else
			$shared = 0;
		
		$this->health_info_model->add_symptom($uid, $symptom_type, $shared);
		echo $seq.'|';
	}
	
	public function delete_symptom($seq, $uid, $symptom_type)
	{
		$this->health_info_model->delete_symptom($uid, $symptom_type);
		echo $seq.'|';
	}
	
	public function get_history_data($seq, $uid, $field)
	{
		$rows = $this->health_info_model->get_history_by($uid, $field);
		if(!$rows)
		{
			echo $seq.'|';
			return;
		}
		echo $seq.'|';
		
		echo json_encode($rows);	
	}
}
?>